package com.a3dweijing.wetouch2.Activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.adapter.ImageShowAdapter;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.ImageList;
import com.a3dweijing.wetouch2.bean.ImageMsg;
import com.a3dweijing.wetouch2.datainterface.Getdataimpl;
import com.google.gson.Gson;

/**
 * Created by yxd on 2017/12/28.
 */

public class ImageShowActivity extends BaseActivity {
    private ImageView back;
    private TextView name;
    private RecyclerView imagerec;
    private Handler handler;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageshow);
        back = (ImageView) findViewById(R.id.back);
        name = (TextView) findViewById(R.id.image_group_name);
        imagerec = (RecyclerView) findViewById(R.id.image_group_rec);
        imagerec.setLayoutManager(new LinearLayoutManager(getBaseContext()));
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case 100:
                        ImageMsg imageMsg=new Gson().fromJson(msg.obj.toString(),ImageMsg.class);
                        if (imageMsg!=null && imageMsg.getThreeDImgList()!=null && imageMsg.getThreeDImgList().size()>0){
                            imagerec.setAdapter(new ImageShowAdapter(imageMsg.getThreeDImgList()));
                        }
                        break;
                    case 404:
                        Toast.makeText(getBaseContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        break;
                }
            }
        };
        ImageList imageList= (ImageList) getIntent().getSerializableExtra("image");
        if (imageList!=null){
            name.setText(imageList.getThree_d_img_name());
        }
        new GetImageThread("1", "1").start();
    }


    private class GetImageThread extends Thread {
        String pageNo;
        String picturetype;

        public GetImageThread(String pageNo, String picturetype) {
            this.pageNo = pageNo;
            this.picturetype = picturetype;
        }

        @Override
        public void run() {
            super.run();
            String resulet = new Getdataimpl().remotGetImageData(pageNo, picturetype);
            if (resulet != null && resulet.length() > 7) {
                handler.sendMessage(handler.obtainMessage(100, resulet));
            } else {
                handler.sendMessage(handler.obtainMessage(404, "网络繁忙，请稍后再试"));
            }
        }


    }
}